package com.itheima.project.web;

import com.itheima.project.basic.ResponseWrap;
import com.itheima.project.core.enums.search.SearchEnum;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.face.DocumentFace;
import com.itheima.project.utils.ResponseWrapBuild;
import com.itheima.project.vo.search.RequestParamVo;
import com.itheima.project.vo.search.SearchPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author itheima
 * @Description 搜索引擎文档操作controller
 */
@Api(tags = "ES文档controller")
@RestController
@RequestMapping("doc")
public class DocumentController {
    @Autowired
    private DocumentFace documentFace;
    /**
     * 保存或修改文档
     */
    @PostMapping("{indexName}/save")
    @ApiOperation(value = "保存或修改文档",notes = "保存或修改文档")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "source",value = "文档对象",required = true,dataType = "Object"),
            @ApiImplicitParam(name = "indexName",value = "索引库名称",required = true,dataType = "String"),
    })
    public ResponseWrap<Boolean> saveDoc(@PathVariable("indexName") String indexName,@RequestBody Map source){
        documentFace.save(source,indexName);
        return ResponseWrapBuild.build(SearchEnum.SUCCEED,true);
    }
    /**
     * 根据id删除es数据
     */
    @DeleteMapping("{indexName}/{id}")
    @ApiOperation(value = "根据ID删除文档",notes = "根据ID删除文档")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "indexName",value = "索引库名称",required = true,dataType = "String"),
            @ApiImplicitParam(name = "id",value = "文档id",required = true,dataType = "String"),
    })
    public ResponseWrap<Boolean> deleteDoc(@PathVariable("indexName") String indexName,@PathVariable("id") String id){
        documentFace.deleteById(id,indexName);
        return ResponseWrapBuild.build(SearchEnum.SUCCEED,true);
    }
    /**
     * 根据id删除es数据
     */
    @GetMapping("{indexName}/{id}")
    @ApiOperation(value = "根据ID查询文档",notes = "根据ID查询文档")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "indexName",value = "索引库名称",required = true,dataType = "String"),
            @ApiImplicitParam(name = "id",value = "文档id",required = true,dataType = "String"),
    })
    public ResponseWrap<Object> getById(@PathVariable("indexName") String indexName,@PathVariable("id") String id){
        return ResponseWrapBuild.build(SearchEnum.SUCCEED,documentFace.getById(id,indexName));
    }
    /**
     * 批量将数据导入es中
     */
    @PostMapping("{indexName}/batch")
    @ApiOperation(value = "批量导入文档到es",notes = "批量导入文档到es")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "indexName",value = "索引库名称",required = true,dataType = "String"),
            @ApiImplicitParam(name = "list",value = "文档数据集合",required = true,dataType = "Object"),
    })
    public ResponseWrap<Boolean> saveDocBatch(@PathVariable("indexName") String indexName,@RequestBody List<Map> list){
        documentFace.saveBatch(list,indexName);
        return ResponseWrapBuild.build(SearchEnum.SUCCEED,true);
    }
    /**
     * 多条件检索
     */
    @PostMapping("{indexName}/search")
    @ApiOperation(value = "根据条件搜索文档",notes = "根据条件搜索文档 RequestParamVo 仿ES ClientAPI封装 语法类似")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "indexName",value = "索引库名称",required = true,dataType = "String"),
            @ApiImplicitParam(name = "paramVo",value = "搜索查询参数",required = true,dataType = "RequestParamVo"),
    })
    public ResponseWrap<SearchPageVo<Object>> searchDoc(@PathVariable("indexName") String indexName,@RequestBody RequestParamVo paramVo){
        SearchPageVo<Object> pageVo = documentFace.search(paramVo, indexName);
        return ResponseWrapBuild.build(SearchEnum.SUCCEED,pageVo);
    }

    /**
     * 多条件检索
     */
    @PostMapping("{indexName}/native-search/page-result")
    @ApiOperation(value = "根据原生条件搜索文档",notes = "根据条件搜索文档 条件通过SearchSourceBuilder构建，toString()得到JSON内容 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "indexName",value = "索引库名称",required = true,dataType = "String"),
            @ApiImplicitParam(name = "searchContent",value = "原生搜索条件json",required = true,dataType = "String"),
    })
    public ResponseWrap<SearchPageVo<Object>> searchDocPageResult(@PathVariable("indexName") String indexName,@RequestBody String searchContent){
        SearchPageVo<Object> pageVo = null;
        try {
            pageVo = documentFace.search(searchContent, indexName);
            return ResponseWrapBuild.build(SearchEnum.SUCCEED,pageVo);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ProjectException(SearchEnum.DOCUMENT_SEARCH_FAIL);
        }
    }

    /**
     * 原生多条件检索
     *
     * return: 返回结果 原生 SearchResponse对应json
     */
    @PostMapping("{indexName}/native-search/native-result")
    @ApiOperation(value = "根据原生条件搜索文档",notes = "根据条件搜索文档 条件通过SearchSourceBuilder构建，toString()得到JSON内容 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "indexName",value = "索引库名称",required = true,dataType = "String"),
            @ApiImplicitParam(name = "searchContent",value = "原生搜索条件json",required = true,dataType = "String"),
    })
    public ResponseWrap<String> searchDocNativeResult(@PathVariable("indexName") String indexName,@RequestBody String searchContent){
        try {
            String result = documentFace.searchNativeResult(searchContent, indexName);
            return ResponseWrapBuild.build(SearchEnum.SUCCEED,result);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ProjectException(SearchEnum.DOCUMENT_SEARCH_FAIL);
        }
    }
}
